package com.zk.iotgateway.classes.loader;

import java.io.*;

public class FileSystemClassLoader extends ClassLoader {

   private String rootDir;

   public FileSystemClassLoader(String rootDir) {
       this.rootDir = rootDir;
   }

   public Class<?> getInstance(String name){
       try{
           return findClass(name);
       }catch (Exception e){
           return  null;
       }
   }

   @Override
   protected Class<?> findClass(String name) throws ClassNotFoundException {
       byte[] classData = getClassData(name);
       if (classData == null) {
           throw new ClassNotFoundException();
       }
       else {
           return defineClass(name, classData, 0, classData.length);
       }
   }

   private byte[] getClassData(String className) {
       String path = classNameToPath(className);
       System.out.println(path);
       try {
           InputStream ins = new FileInputStream(path);
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           int bufferSize = 4096;
           byte[] buffer = new byte[bufferSize];
           int bytesNumRead = 0;
           while ((bytesNumRead = ins.read(buffer)) != -1) {
               baos.write(buffer, 0, bytesNumRead);
           }
           return baos.toByteArray();
       } catch (IOException e) {
           e.printStackTrace();
       }
       return null;
   }

   private String classNameToPath(String className) {
       rootDir =this.getClass().getResource("/").getPath();
       System.out.println(rootDir);
       return rootDir + File.separatorChar
               + className.replace('.', File.separatorChar) + ".class";
   }
}